package Controls
{
	import Easing.Circ;
	
	import Objects.GameBitmap;
	import Objects.GameObject;
	import Objects.GameSprite;
	
	import Tools.BitmapTool;
	import Input.GameInput;
	
	import Tween.AlphaTween;
	import Tween.MoveTween;
	import Tween.ScaleTween;
	
	import flash.display.BitmapData;
	import flash.geom.Point;

	public class DragProxy extends GameSprite
	{
		public var go:GameObject;
		public var image:GameBitmap;
		private var handleX:Number=0;
		private var handleY:Number=0;
		private var waitDestroy:Boolean=false;
		public var status:GameBitmap;
		public var acceptImage:BitmapData;
		public var rejectImage:BitmapData;
		public function DragProxy()
		{
			super();
			
			statistical=true;
			intelligent=true;
			checkMouse=false;
			
			image=new GameBitmap();
			addChild(image);

			status=new GameBitmap();
			addChild(status);
		}
		override protected function $update():void
		{
			if(!waitDestroy)
			{
				var acceptObject:GameObject=findAcceptObject();
				if(acceptObject)
				{
					cutoverStatus(0);
				}
				else
				{
					cutoverStatus(1);
				}
				moveTo(GameInput.i.mousex,GameInput.i.mousey,handleX,handleY);
				if(!GameInput.i.mDown)
				{
					var acceptObject:GameObject=findAcceptObject();
					if(acceptObject)
					{
						moveToTarget(acceptObject);
						acceptObject.acceptDrag(go);
					}
					else
					{
						moveToSource(go);
					}
					waitDestroy=true;
				}
					
			}
		}
		protected function cutoverStatus(value:int):void
		{
			switch(value)
			{
				case 0:
					status.setImage(acceptImage);
					break;
				case 1:
					status.setImage(rejectImage);
					break;
			}
			status.moveTo(image.obj.width,image.obj.height,1,1);
		}
		private function findAcceptObject():GameObject
		{
			var cacheOvers:Array=gs.cacheOvers;
			for each(var temp:GameObject in cacheOvers)
			{
				if(temp.verifyDrag(go))
				{
					return temp;
				}
			}
			return null;
		}
		private function moveToTarget(target:GameObject):void
		{
			var moveTween:MoveTween=new MoveTween();
			moveTween.easeing=Circ.easeOut;
			moveTween.go=this;
			moveTween.duration=25;
			moveTween.startX=obj.x;
			moveTween.startY=obj.y;
			moveTween.endX=target.globalBounds.x+target.globalBounds.width/2;
			moveTween.endY=target.globalBounds.y+target.globalBounds.height/2;
			moveTween.finishCallBack=finishMove;
			gs.addPlugin(moveTween);
			
			var scaleTween:ScaleTween=new ScaleTween();
			scaleTween.easeing=Circ.easeOut;
			scaleTween.go=this;
			scaleTween.duration=25;
			scaleTween.startScaleX=1;
			scaleTween.startScaleY=1;
			scaleTween.endScaleX=0;
			scaleTween.endScaleY=0;
			gs.addPlugin(scaleTween);
			
			var alphaTween:AlphaTween=new AlphaTween();
			alphaTween.easeing=Circ.easeOut;
			alphaTween.go=this;
			alphaTween.duration=25;
			alphaTween.startAlpha=obj.alpha;
			alphaTween.endAlpha=0;
			gs.addPlugin(alphaTween);
		}
		private function moveToSource(source:GameObject):void
		{
			var moveTween:MoveTween=new MoveTween();
			moveTween.easeing=Circ.easeOut;
			moveTween.go=this;
			moveTween.duration=25;
			moveTween.startX=obj.x;
			moveTween.startY=obj.y;
			moveTween.endX=source.globalBounds.x;
			moveTween.endY=source.globalBounds.y;
			moveTween.finishCallBack=finishMove;
			gs.addPlugin(moveTween);
		}
		private function finishMove():void
		{
			go.dragProxy=null;
			remove();
			waitDestroy=false;
		}
		public final function setup():void
		{
			handleX=go.obj.mouseX/go.obj.width;
			handleY=go.obj.mouseY/go.obj.height;
			var bitmapData:BitmapData=new BitmapData(go.obj.width,go.obj.height,true,0);
			BitmapTool.draw(bitmapData,go.obj,0,0);
			image.setImage(bitmapData);
			_setup();
		}
		protected function _setup():void
		{
			
		}
	}
}